package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShSpecialCoinDao;
import com.shop.shoppingmall.entity.ShSpecialCoin;
import com.shop.shoppingmall.service.ShSpecialCoinService;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专用币明细(ShSpecialCoin)表服务实现类
 *
 * @author makejava
 * @since 2020-09-11 10:59:51
 */
@Service("shSpecialCoinService")
@Transactional
public class ShSpecialCoinServiceImpl implements ShSpecialCoinService {
    @Resource
    private ShSpecialCoinDao shSpecialCoinDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShSpecialCoin queryById(String id) {
        return this.shSpecialCoinDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param shSpecialCoin 实例对象
     * @return 实例对象
     */
    @Override
    public ShSpecialCoin insert(ShSpecialCoin shSpecialCoin) {
        this.shSpecialCoinDao.insert(shSpecialCoin);
        return shSpecialCoin;
    }

    /**
     * 修改数据
     *
     * @param shSpecialCoin 实例对象
     * @return 实例对象
     */
    @Override
    public ShSpecialCoin update(ShSpecialCoin shSpecialCoin) {
        this.shSpecialCoinDao.update(shSpecialCoin);
        return this.queryById(shSpecialCoin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.shSpecialCoinDao.deleteById(id) > 0;
    }

    /**
     * 专用币收入明细
     * @param income
     * @param userId
     * @param detail
     * @return
     */
    @Override
    public Result insertSpecialCoin(BigDecimal income, String userId, String detail) {
       ShSpecialCoin shSpecialCoin = new ShSpecialCoin();
        shSpecialCoin.setId(UUIDGenerator.generate());
        shSpecialCoin.setIncome(income);
        shSpecialCoin.setUserId(userId);
        shSpecialCoin.setType("1");
        shSpecialCoin.setDetail(detail);
        shSpecialCoinDao.insert(shSpecialCoin);
        return Result.ok();
    }

    /**
     * 添加积分支出明细纪录
     * @param expenditure 支出金额
     * @param userId  用户id
     * @param detail  备注
     * @return
     */
    @Override
    public Result insertExpenditure(BigDecimal expenditure, String userId, String detail) {
        ShSpecialCoin shSpecialCoin = new ShSpecialCoin();
        shSpecialCoin.setId(UUIDGenerator.generate());
        shSpecialCoin.setExpenditure(expenditure);
        shSpecialCoin.setUserId(userId);
        shSpecialCoin.setType("2");
        shSpecialCoin.setDetail(detail);
        shSpecialCoinDao.insert(shSpecialCoin);
        return Result.ok();
    }

    /**
     * 查询馨天专用币明细纪录
     * @param map
     * @return
     */
    @Override
    public Result queryAll(Map map) {
        List<ShSpecialCoin> specialCoinList = shSpecialCoinDao.queryAll(map);
        return Result.ok(specialCoinList);

    }

    @Override
    public Result querySpecialCoin(Map map) {
        Integer pagenum = (Integer) map.get("pagenum");
        Integer pagesize = (Integer) map.get("pagesize");
        int a = (pagenum-1)*pagesize;
        map.put("offset",a);
        map.put("limit",pagesize);
        List<ShSpecialCoin> shSpecialCoins = shSpecialCoinDao.querySpecialCoin(map);
        if(shSpecialCoins == null){
            return Result.error("暂时还没有专用币明细的信息！");
        }
        ShSpecialCoin shSpecialCoin = shSpecialCoinDao.countSpecialCoin(map);
        Map map1 = new HashMap();
        map1.put("total", shSpecialCoin.getSums());
        map1.put("shSpecialCoins",shSpecialCoins);
        return Result.ok(map1);
    }
}
